home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wtj008.zip / PORTING.ZIP / SAMPLE.C < prev    next >
C/C++ Source or Header  |  1992-06-08  |  12KB  |  420 lines

  1. #include "windows.h"
  2. #include "sample.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7.  
  8. HANDLE hInst;
  9. HWND hMainWnd;
  10. HWND hDlgModeless  = 0;             // handle for modeless dialogs.
  11. HCURSOR hHourGlass;
  12.  
  13. long *dwM, *dwN, *dwIACK, *dwIPT, *dwABORT;
  14.  
  15. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance,
  16.                     LPSTR lpCmdLine, int nCmdShow )
  17. {
  18.    MSG msg;
  19.  
  20.    hHourGlass = LoadCursor(NULL, IDC_WAIT);
  21.  
  22.    if (!hPrevInstance)                  // Other instances of app running?
  23.       if (!InitApplication(hInstance)) // Initialize shared things
  24.          return (FALSE);              // Exit if unable to initialize
  25.  
  26.    // Perform initializations that apply to a specific instance
  27.  
  28.    if (!InitInstance( hInstance, nCmdShow ))
  29.       return( FALSE );
  30.       
  31.    // Get, translate, and dispatch messages until WM_QUIT is received
  32.  
  33.  
  34.    // Message loop modified to work with modeless dialog box
  35.  
  36.    while (GetMessage(&msg, NULL, NULL, NULL)) {
  37.       if (hDlgModeless == 0 || !IsDialogMessage(hDlgModeless, &msg)) {
  38.          TranslateMessage(&msg);
  39.          DispatchMessage(&msg);
  40.       }
  41.    }
  42.  
  43.    return( msg.wParam );
  44. }
  45.  
  46. // Function: InitApplication
  47. // Initialize the application: fill in the window-class structure
  48. // and register the window class. Return TRUE on success, FALSE
  49. // on failure.
  50.  
  51. BOOL InitApplication (HANDLE hInstance)
  52. {
  53.     WNDCLASS  wc;
  54.  
  55.     wc.style = NULL;
  56.     wc.lpfnWndProc = MainWndProc;
  57.     wc.cbClsExtra = 0;
  58.     wc.cbWndExtra = 0;
  59.     wc.hInstance = hInstance;
  60.     wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  61.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  62.     wc.hbrBackground = GetStockObject( WHITE_BRUSH );
  63.     wc.lpszMenuName =  "MainMenu";
  64.     wc.lpszClassName = "SampleWinClass";
  65.  
  66.     return (RegisterClass(&wc));
  67.  
  68. }
  69.  
  70. // Function: InitInstance
  71. // Save the instance handle and create the main window for this instance.
  72.  
  73. BOOL InitInstance (HANDLE hInstance, int nCmdShow)
  74. {
  75.     HWND hWnd;
  76.  
  77.     hInst = hInstance;
  78.  
  79.     // Create a main window for this application instance.
  80.  
  81.     hWnd = CreateWindow("SampleWinClass", "DLL Example",
  82.         WS_OVERLAPPEDWINDOW,
  83.         CW_USEDEFAULT, CW_USEDEFAULT,
  84.         CW_USEDEFAULT, CW_USEDEFAULT,
  85.         NULL, NULL, hInstance, NULL);
  86.  
  87.     // If window could not be created, return "failure"
  88.  
  89.     if( !hWnd )
  90.         return( FALSE );
  91.     else
  92.       hMainWnd = hWnd;     // save for future reference!
  93.  
  94.     // Make the window visible; update its client area; and return "success"
  95.  
  96.     ShowWindow(hWnd, nCmdShow);
  97.     UpdateWindow(hWnd);
  98.     return(TRUE);
  99.  
  100. }
  101.  
  102. // Function: MainWndProc
  103. // Process messages sent to the main window. Display results
  104. // when user clicks the "Ok" button on the "Multiply" dialog box.
  105.  
  106. long FAR PASCAL MainWndProc (HWND hWnd, unsigned message,
  107.                              WORD wParam, LONG lParam)
  108. {
  109.    FARPROC lpProc;          // pointer to dialog-box functions
  110.    static FARPROC lpfnSetAddrI;
  111.  
  112.    switch( message ) {
  113.    case WM_CREATE:
  114.       
  115.       init_window(hWnd);
  116.       
  117.       lpfnSetAddrI = MakeProcInstance(SetAddrI, hInst);
  118.       init_lpsetI(lpfnSetAddrI);
  119.             
  120.       setupDLL();
  121.       if (!checkLinks(hWnd))
  122.          SendMessage (hWnd, WM_QUIT, 0, 0L);
  123.       break;   
  124.     case WM_COMMAND:       // message: command from application menu
  125.         switch( wParam ) {
  126.         case IDM_ABOUT:
  127.             lpProc = MakeProcInstance(AboutBoxFunc, hInst);
  128.             DialogBox(hInst, "AboutBox", hWnd, lpProc);
  129.             FreeProcInstance(lpProc);
  130.             break;
  131.         case IDM_EXIT:
  132.            DestroyWindow (hWnd);
  133.            return(0);
  134.         case IDM_SETV:
  135.             lpProc = MakeProcInstance(SetupFunc, hInst);
  136.             DialogBox(hInst, "Setup", hWnd, lpProc);
  137.             FreeProcInstance(lpProc);
  138.             break;
  139.          case IDM_DOIT:
  140.             hDlgModeless = CreateDialog(hInst, "DoIt", hWnd,
  141.                            MakeProcInstance(DoItFunc, hInst));
  142.             break;
  143.         default:
  144.             return(DefWindowProc(hWnd, message, wParam, lParam));
  145.         }
  146.         break;
  147.  
  148.     case WM_DESTROY:
  149.         PostQuitMessage(0);
  150.         break;
  151.  
  152.     default:
  153.         return(DefWindowProc(hWnd, message, wParam, lParam));
  154.     }
  155.     return(NULL);
  156. }
  157.  
  158. BOOL FAR PASCAL AboutBoxFunc(
  159.                       HWND hDlg,        // window handle of the dialog box
  160.                       unsigned message, // type of message
  161.                       WORD     wParam,  // message-specific information
  162.                       LONG     lParam )
  163. {
  164.     switch( message )
  165.     {
  166.  
  167.     case WM_INITDIALOG:             // message: initialize dialog box
  168.         return( TRUE );
  169.  
  170.     case WM_COMMAND:                      // message: received a command
  171.         if( wParam == IDOK                // "OK" box selected?
  172.             || wParam == IDCANCEL)        // System menu close command?
  173.         {
  174.             EndDialog( hDlg, TRUE );      // Exits the dialog box
  175.             return( TRUE );
  176.         }
  177.     break;
  178.     }
  179.     return( FALSE );               // Didn't process a message
  180. } // AboutBoxFunc
  181.  
  182. BOOL FAR PASCAL SetupFunc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  183. {
  184.    BOOL lpTrans;
  185.    long n1,n2;
  186.    
  187.    switch (message) {
  188.    case WM_INITDIALOG:
  189.       SetDlgItemInt(hDlg, IDD_PARAM1, (WORD)*dwM, FALSE);
  190.       SetDlgItemInt(hDlg, IDD_PARAM2, (WORD)*dwN, FALSE);
  191.       return (TRUE);
  192.  
  193.    case WM_COMMAND:
  194.       switch (wParam) { 
  195.       case IDOK:
  196.          n1 = (long) GetDlgItemInt (hDlg, IDD_PARAM1, &lpTrans, 0);
  197.          if (!lpTrans) {
  198.              MessageBox (hDlg, "Invalid value for first parameter",
  199.                                  NULL, MB_ICONHAND | MB_OK);
  200.              return(FALSE);
  201.          }
  202.          else
  203.             *dwM = n1;
  204.             
  205.          n2 = (long) GetDlgItemInt (hDlg, IDD_PARAM2, &lpTrans, 0);
  206.          if (!lpTrans) {
  207.              MessageBox (hDlg, "Invalid value for second parameter",
  208.                                  NULL, MB_ICONHAND | MB_OK);
  209.              return(FALSE);
  210.          }
  211.          else
  212.             *dwN = n2;
  213.             
  214.          EndDialog (hDlg, TRUE);
  215.          return (TRUE);
  216.  
  217.       case IDCANCEL:
  218.          EndDialog (hDlg, TRUE);
  219.          return (TRUE);
  220.       }
  221.    break;
  222.    }
  223.    return (FALSE);
  224. }
  225.  
  226. BOOL FAR PASCAL DoItFunc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  227. {
  228.    static int exitAction = 0;
  229.    BOOL bDoingSomething;
  230.    char buff[255];
  231.    HCURSOR hSaveCursor;
  232.    
  233.    switch (message) {
  234.    case WM_COMMAND:
  235.       switch (wParam) {
  236.       case IDD_CANCEL:
  237.          // if the user presses 'cancel' when we're doing a calculation,
  238.          // tell the DLL and carry on...
  239.          // If we're not doing anything, though, close the dialog window.
  240.          
  241.          if (bDoingSomething) {
  242.          
  243.             // the 'winabort' flag is raised when we want to exit from the
  244.             // calculation, and is checked within the DLL every iteration.
  245.             // If the DLL finds this flag is non-zero, it causes
  246.             // an exit back up the call chain, eventually returning control
  247.             // to the IDD_DOIT case, after the call to alloc().
  248.             
  249.             *dwABORT = 1;
  250.          }
  251.          else {
  252.             *dwABORT = 0;
  253.             DestroyWindow(hDlg);
  254.             hDlgModeless = 0;
  255.          }
  256.          return (TRUE);
  257.  
  258.       // Perform the calculation... when done, it changes the button
  259.       // text so the user can press it to exit.
  260.  
  261.       case IDD_DOIT:
  262.  
  263.          // if we're exiting, go and do it...
  264.  
  265.          if (exitAction == 1) {
  266.             DestroyWindow(hDlg);
  267.             hDlgModeless = 0;
  268.             return (TRUE);
  269.          }
  270.  
  271.          // do the allocation.... disable the button first...
  272.          
  273.          EnableWindow(GetDlgItem(hDlg, IDD_DOIT), FALSE);
  274.          
  275.          sprintf(buff,"Calculating...");
  276.          SetDlgItemText (hDlg, IDD_MSG, buff);
  277.          hSaveCursor = SetCursor(hHourGlass);
  278.          bDoingSomething = TRUE;
  279.          ackerman();
  280.          SetCursor(hSaveCursor);
  281.          bDoingSomething = FALSE;
  282.  
  283.          // done - so disable the 'cancel' button, and enable the
  284.          // 'exit' button
  285.  
  286.          SetWindowText(GetDlgItem(hDlg, IDD_DOIT), "Exit");
  287.          EnableWindow(GetDlgItem(hDlg, IDD_DOIT), TRUE);
  288.          EnableWindow(GetDlgItem(hDlg, IDD_CANCEL), FALSE);
  289.          
  290.          if (*dwABORT == 1) {
  291.             sprintf(buff,"Calculation was interrupted!");
  292.             SetDlgItemText (hDlg, IDD_MSG, buff);
  293.          }
  294.          else {
  295.             sprintf(buff,"Result: %ld",*dwIACK);
  296.             SetDlgItemText (hDlg, IDD_MSG, buff);
  297.          }
  298.  
  299.          exitAction = 1;
  300.          
  301.          SetFocus(hDlg);
  302.  
  303.          return(TRUE);
  304.       }
  305.       break;
  306.  
  307.    case WM_INITDIALOG:
  308.       // set the flags - 
  309.       // bDoingSomething is set when an allocation is in progress,
  310.       // and affects the repainting and cancel behaviour.
  311.       // dwABORT is a pointer to the DLLs abort flag; the DLL checks
  312.       // this flag each time thru the iteration loop, and will bail out if 
  313.       // it sees this set non-zero. SO it's important to set it to zero when
  314.       // we start!
  315.       // exitAction is a flag showing the state of the IDD_DOIT button, which
  316.       // will show 'Do It!' on entry, and then 'Exit' when the calculation
  317.       // has finished.
  318.       
  319.       bDoingSomething = FALSE;
  320.       *dwABORT = 0;
  321.       exitAction = 0;
  322.       
  323.       // pass the window handle to the DLL; this causes the c_update_window
  324.       // routine in the DLL to update the right window....
  325.          
  326.       init_window(GetDlgItem(hDlg,IDD_MSG));
  327.  
  328.       return (TRUE);
  329.    }
  330.    return (FALSE);
  331. }
  332.  
  333. // SetAddrI is a callback function which the DLL uses to pass over
  334. // the addresses of ints
  335.  
  336. BOOL FAR PASCAL SetAddrI (LPSTR var, long *n)
  337. {
  338.    char szBuffer[128];
  339.  
  340.    // print out variable as a check
  341.    wsprintf (szBuffer, "Variable to set = %s", var);
  342.    MessageBox  (NULL, szBuffer, "SetAddrI", MB_OK);
  343.  
  344.    if (!lstrcmp(var, (LPSTR)"m")) {
  345.       dwM = n;
  346.       wsprintf (szBuffer, "m = %ld", *dwM);
  347.       MessageBox (NULL, szBuffer, "SetAddrI", MB_OK);
  348.    }
  349.    else if (!lstrcmp(var, (LPSTR)"n")) {
  350.       dwN = n;
  351.       wsprintf (szBuffer, "n = %ld", *dwN);
  352.       MessageBox (NULL, szBuffer, "SetAddrI", MB_OK);
  353.    }
  354.    else if (!lstrcmp(var, (LPSTR)"iack")) {
  355.       dwIACK = n;
  356.       wsprintf (szBuffer, "iack = %ld", *dwIACK);
  357.       MessageBox (NULL, szBuffer, "SetAddrI", MB_OK);
  358.    }
  359.    else if (!lstrcmp(var, (LPSTR)"ipt")) {
  360.       dwIPT = n;
  361.       wsprintf (szBuffer, "ipt = %ld", *dwIPT);
  362.       MessageBox (NULL, szBuffer, "SetAddrI", MB_OK);
  363.    }
  364.    else if (!lstrcmp(var, (LPSTR)"abort")) {
  365.       dwABORT = n;
  366.       wsprintf (szBuffer, "abort = %ld", *dwABORT);
  367.       MessageBox (NULL, szBuffer, "SetAddrI", MB_OK);
  368.    }
  369.    else {
  370.       wsprintf (szBuffer, "Unrecognised variable = %s", var);
  371.       MessageBox (NULL, szBuffer, "SetAddrI", MB_OK);
  372.    }
  373.    
  374.    return(TRUE);
  375. }
  376.  
  377. // checkLinks does a check on all the pointers which should have been
  378. // set up by the DLL calling back.... if any are still null, it'll
  379. // complain.
  380.  
  381. BOOL checkLinks (HWND hWnd)
  382. {
  383.    BOOL bretval;
  384.    char szVarName[80], szBuff[255];
  385.  
  386.    bretval = TRUE;
  387.  
  388.    // integers...
  389.  
  390.    if (dwM == NULL) {
  391.       strcpy(szVarName,"M");
  392.       bretval = FALSE;
  393.    }
  394.    if (dwN == NULL) {
  395.       strcpy(szVarName,"N");
  396.       bretval = FALSE;
  397.    }
  398.    if (dwIACK == NULL) {
  399.       strcpy(szVarName,"IACK");
  400.       bretval = FALSE;
  401.    }
  402.    if (dwIPT == NULL) {
  403.       strcpy(szVarName,"IPT");
  404.       bretval = FALSE;
  405.    }
  406.    if (dwABORT == NULL) {
  407.       strcpy(szVarName,"ABORT");
  408.       bretval = FALSE;
  409.    }
  410.  
  411.    if (bretval == FALSE) {
  412.       sprintf(szBuff,
  413.         "Variable %s unlinked.\nThis is an internal program error.", 
  414.         szVarName);
  415.       MessageBox (hWnd, szBuff, "Fatal Data Link Error", MB_OK | MB_ICONSTOP);
  416.    }
  417.  
  418.    return(bretval);
  419. }
  420.